message("========== DNNL ==========")
message("Use onednn from submodule")
set(DNNL_BLAS_VENDOR "NONE" CACHE STRING "")
if (${RUNTIME_THREAD} STREQUAL "TBB")
    set(DNNL_CPU_RUNTIME "TBB")
elseif(${RUNTIME_THREAD} STREQUAL "OMP")
    set(DNNL_CPU_RUNTIME "OMP")
endif()
set(DNNL_LIBRARY_TYPE "STATIC" CACHE STRING "")
set(DNNL_ENABLE_CONCURRENT_EXEC ON CACHE BOOL "")
set(DNNL_BUILD_EXAMPLES OFF CACHE BOOL "")
set(DNNL_BUILD_TESTS OFF CACHE BOOL "")
# for ArmCL backend
if(ENABLE_ARMCL)
    message("DNNL_AARCH64_USE_ACL ARMCL_INSTALL:${ARMCL_INSTALL} PROJECT_SOURCE_DIR:${PROJECT_SOURCE_DIR}")
    set(DNNL_AARCH64_USE_ACL "ON")
    set(ACL_ROOT_DIR ${ARMCL_INSTALL})
    set(ACL_INCLUDE_DIR ${ARMCL_INSTALL}/include)
    set(ACL_LIBRARY ${ARMCL_INSTALL}/lib/libarm_compute-static.a)
    set(ACL_GRAPH_LIBRARY ${ARMCL_INSTALL}/lib/libarm_compute_graph-static.a)
    set(ACL_CORE_LIBRARY ${ARMCL_INSTALL}/lib/libarm_compute_core-static.a)
    
    # cmake_policy(SET CMP0077 NEW)
    list(APPEND ALLSPARK_DEFINITION "-DDNNL_AARCH64_USE_ACL=ON")
endif()
set(DNNL_INSTALL ${INSTALL_LOCATION}/dnnl/install)

include(ExternalProject)

if(CONFIG_HOST_CPU_TYPE STREQUAL "ARM")
    ExternalProject_Add(project_dnnl
        PREFIX ${CMAKE_CURRENT_BINARY_DIR}/dnnl
        URL ${CMAKE_SOURCE_DIR}/third_party/dnnl.tar.bz2
        #DOWNLOAD_EXTRACT_TIMESTAMP 1680675044
        CONFIGURE_HANDLED_BY_BUILD ON
        # PATCH_COMMAND ${MKLDNN_PATCH_DISCARD_COMMAND} COMMAND ${DNNL_PATCH_COMMAND}
        #SOURCE_DIR ${DNNL_SOURCE}
        # CMAKE_ARGS -DACL_GRAPH_LIBRARY=${ACL_GRAPH_LIBRARY} -DACL_CORE_LIBRARY=${ACL_CORE_LIBRARY} -DACL_ROOT_DIR=${ACL_ROOT_DIR} -DDNNL_AARCH64_USE_ACL=${DNNL_AARCH64_USE_ACL} -DACL_INCLUDE_DIR=${ACL_INCLUDE_DIR} -DACL_LIBRARY=${ACL_LIBRARY} -DDNNL_BUILD_TESTS=OFF -DDNNL_CPU_RUNTIME=${DNNL_CPU_RUNTIME} -DDNNL_LIBRARY_TYPE=${DNNL_LIBRARY_TYPE} -DDNNL_ENABLE_CONCURRENT_EXEC=ON -DDNNL_BUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DCMAKE_INSTALL_PREFIX=${DNNL_INSTALL} ${DNNL_GPU_CMAKE_ARGS}
        CMAKE_ARGS -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=AARCH64 -DONEDNN_BUILD_TESTS=OFF -DONEDNN_CPU_RUNTIME=${DNNL_CPU_RUNTIME} -DONEDNN_LIBRARY_TYPE=${DNNL_LIBRARY_TYPE} -DONEDNN_ENABLE_CONCURRENT_EXEC=ON -DONEDNN_BUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DCMAKE_INSTALL_PREFIX=${DNNL_INSTALL} ${DNNL_GPU_CMAKE_ARGS}
      )
else()
    find_program(CCACHE_PROGRAM ccache)
    if(CCACHE_PROGRAM)
        message("use ccache compile dnnl")
        ExternalProject_Add(project_dnnl
        PREFIX ${CMAKE_CURRENT_BINARY_DIR}/dnnl
        URL ${CMAKE_SOURCE_DIR}/third_party/dnnl.tar.bz2
        CONFIGURE_HANDLED_BY_BUILD ON
        BUILD_COMMAND ${CMAKE_COMMAND} --build . -j16
        CMAKE_ARGS -DONEDNN_BUILD_TESTS=OFF -DONEDNN_CPU_RUNTIME=${DNNL_CPU_RUNTIME} -DCMAKE_CXX_COMPILER_LAUNCHER=ccache -DONEDNN_LIBRARY_TYPE=${DNNL_LIBRARY_TYPE} -DONEDNN_ENABLE_CONCURRENT_EXEC=ON -DONEDNN_BUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DCMAKE_INSTALL_PREFIX=${DNNL_INSTALL} ${DNNL_GPU_CMAKE_ARGS}
      )
    else()
    ExternalProject_Add(project_dnnl
        PREFIX ${CMAKE_CURRENT_BINARY_DIR}/dnnl
        URL ${CMAKE_SOURCE_DIR}/third_party/dnnl.tar.bz2
        #DOWNLOAD_EXTRACT_TIMESTAMP 1680675044
        CONFIGURE_HANDLED_BY_BUILD ON
        BUILD_COMMAND     ${CMAKE_COMMAND} --build . -j16
        # PATCH_COMMAND ${MKLDNN_PATCH_DISCARD_COMMAND} COMMAND ${DNNL_PATCH_COMMAND}
        #SOURCE_DIR ${DNNL_SOURCE}
        # CMAKE_ARGS -DACL_GRAPH_LIBRARY=${ACL_GRAPH_LIBRARY} -DACL_CORE_LIBRARY=${ACL_CORE_LIBRARY} -DACL_ROOT_DIR=${ACL_ROOT_DIR} -DDNNL_AARCH64_USE_ACL=${DNNL_AARCH64_USE_ACL} -DACL_INCLUDE_DIR=${ACL_INCLUDE_DIR} -DACL_LIBRARY=${ACL_LIBRARY} -DDNNL_BUILD_TESTS=OFF -DDNNL_CPU_RUNTIME=${DNNL_CPU_RUNTIME} -DDNNL_LIBRARY_TYPE=${DNNL_LIBRARY_TYPE} -DDNNL_ENABLE_CONCURRENT_EXEC=ON -DDNNL_BUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DCMAKE_INSTALL_PREFIX=${DNNL_INSTALL} ${DNNL_GPU_CMAKE_ARGS}
        CMAKE_ARGS -DONEDNN_BUILD_TESTS=OFF -DONEDNN_CPU_RUNTIME=${DNNL_CPU_RUNTIME} -DONEDNN_LIBRARY_TYPE=${DNNL_LIBRARY_TYPE} -DONEDNN_ENABLE_CONCURRENT_EXEC=ON -DONEDNN_BUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} -DCMAKE_INSTALL_PREFIX=${DNNL_INSTALL} ${DNNL_GPU_CMAKE_ARGS}      )
    endif()
endif()

ExternalProject_Add_Step(
    project_dnnl unzip
    COMMAND ${CMAKE_COMMAND} -E tar jxf ${CMAKE_SOURCE_DIR}/third_party/dnnl.tar.bz2
    DEPENDEES download
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/
)

# make include not report error during configure phase.
file(MAKE_DIRECTORY ${DNNL_INSTALL}/include)

#add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/dnnl EXCLUDE_FROM_ALL)
set(ONEDNN_LIBRARY external_dnnl)

add_library(external_dnnl STATIC IMPORTED GLOBAL)

set(FILE_PATH "/etc/os-release")
file(STRINGS ${FILE_PATH} ID_LINE REGEX "^ID=.*$")

if(ID_LINE)
    string(REGEX REPLACE "^ID=(.*)$" "\\1" OS_NAME ${ID_LINE})
    # ~S~G~S~^~\
    message(STATUS "The OS ID is: ${OS_NAME}")
else()
    message(FATAL "No ID line found in file.")
endif()

string (REGEX MATCH "ubuntu" IS_UBUNTU ${OS_NAME})

if(NOT IS_UBUNTU AND CMAKE_SIZEOF_VOID_P EQUAL 8)
    if(CONFIG_HOST_CPU_TYPE STREQUAL "ARM")
        set(LIB_ARCH)
    else()
        message("build system not ubuntu ${OS_NAME}")
        set(LIB_ARCH 64)
    endif()
else()
    message("build system is ubuntu ${OS_NAME}")
    set(LIB_ARCH )
endif()

set_target_properties(external_dnnl PROPERTIES IMPORTED_LOCATION ${DNNL_INSTALL}/lib${LIB_ARCH}/libdnnl.a
    INTERFACE_INCLUDE_DIRECTORIES ${DNNL_INSTALL}/include)

add_dependencies(external_dnnl project_dnnl)

message("==========================")
